package plus.util;

import com.baomidou.mybatisplus.core.metadata.IPage;
import common.core.util.CommonUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

/**
 * @author liubo
 * @created 2019-12-27 0:10
 */
public class Condition extends HashMap<String,Object>{

    public static Condition builder(){
        return new Condition();
    }

    public Condition id(Serializable id){
        this.put(ConditionHelper.key_id,id);
        return this;
    }

    public Condition ids(Serializable... ids){
        List<Object> idList=new ArrayList();
        for (Serializable id : ids) {
            idList.add(id);
        }
        this.put(ConditionHelper.key_idList,idList);
        return this;
    }

    public Condition eq(String cloumn, Object value){
        ArrayList<ConditionInfo> list = (ArrayList<ConditionInfo>) this.get(ConditionHelper.key_wrapper);
        if(CommonUtil.isEmpty(list)){
            list = new ArrayList<>();
        }
        ConditionInfo info = new ConditionInfo(cloumn,value,ConditionHelper.key_operator_eq);
        list.add(info);
        this.put(ConditionHelper.key_wrapper,list);
        return this;
    }
    public Condition eq(String cloumn, Object value,boolean isTrue){
        if(isTrue){
            return this;
        }
        return eq(cloumn,value);
    }

    public Condition entitys(Collection entitys){
        this.put(ConditionHelper.key_entitys,entitys);
        return this;
    }

    public Condition page(IPage page,Condition condition){
        this.put(ConditionHelper.key_page,page);
        this.put(ConditionHelper.key_condition,condition);
        return this;
    }
    public Condition page(IPage page){
        return page(page,null);
    }
}
